﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.Member
{
    public partial class LstMemberNangCap : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private string email;
        private int cPage = 1;
        private int page;
        private string codeMember;
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                page = int.Parse("0" + Request.QueryString["page"]);
                if (page == 0) page = 1;
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);
                if (Request.QueryString["codeMember"] == "" || Request.QueryString["codeMember"] == null)
                {
                    txtCodeMember.Text = "0";
                }
                else
                {
                    txtCodeMember.Text =Request.QueryString["codeMember"];
                    codeMember = txtCodeMember.Text;
                }
                if (Request.QueryString["email"] == "" || Request.QueryString["email"] == null)
                {
                    txtKeysearch.Text = "";
                }
                else
                {
                    txtKeysearch.Text = Request.QueryString["email"];
                    email = txtKeysearch.Text;
                }

               LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        #endregion


        #region LoadData
        private void LoadData()
        {

            totalRecords = UpgradeAccount.SelectCount(codeMember,email);
            lbTotal.Text = "Tổng số bản ghi: " + totalRecords;
            DataTable dt;
            if (Request.QueryString.Get("cpage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            if (cPage == 0) cPage = 1;
            int from = (cPage - 1) * Const.pageSize20 + 1;
            if (from == 0) from = 1;
            int to = (cPage) * Const.pageSize20;

            PagingAjax1.totalRecords = totalRecords;
            PagingAjax1.numPerPage = Const.pageSize20;
            PagingAjax1.queryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "IdMember", "Email", "from", "to", "cPage" },
                                    new string[] { txtCodeMember.Text,txtKeysearch.Text, from.ToString(), to.ToString(), "" });

            PagingAjax1.defaultQueryParam =
                ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "IdMember", "Email", "from", "to" },
                                    new string[] { txtCodeMember.Text,txtKeysearch.Text,from.ToString(), to.ToString() });

            string order = null;
            dt = UpgradeAccount.Search(codeMember,email, from, to, order);
            if (dt != null && dt.Rows.Count > 0)
            {

                grdLstMemberNangcap.DataSource = dt;
                grdLstMemberNangcap.RowDataBound += new GridViewRowEventHandler(grdLstMemberNangcap_RowDataBound);
                grdLstMemberNangcap.DataBind();

            }
        }
        #endregion

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey + "&email=" + txtKeysearch.Text + "&codeMember=" + txtCodeMember.Text );
        }

        protected void grdLstMemberNangcap_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           if (e.Row.RowType == DataControlRowType.DataRow)
            {
              e.Row.Cells[0].Text = (Const.pageSize20 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
               
            }
        }

        public static string Get_Status(int status)
        {
            string strMessage = "";
            if (status == 1)
            {
                strMessage = "Kim cương";
            }
            else if (status == 2)
            {
                strMessage = "Vàng";
            }
            else if (status == 3)
            {
                strMessage = "Bạc";
            }

            return strMessage;
        }

      
    }
}